Shortest possible match with regexp

Doors Regexp gives the longest possible match of the regular expression.
Is it possible to get the shortest possible match?

Example:

string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
string sCommentReg    = "Result\\((.*)\\)\\,"
Regexp regComment       = regexp sCommentReg


Gives back "Foo1), Issue(Foo2), Location(Foo3". I need only "Foo1".
OK, I know that "Result\\((.*)\\)\\, Issue" would solve the problem, but what if I change the order.

Thank You,

Gabor


GaborMoizes - Thu Feb 24 12:05:49 EST 2011

Re: Shortest possible match with regexp
GaborMoizes - Thu Feb 24 12:08:22 EST 2011

I cannot modify the post, so I re-post with modification:

Doors Regexp gives the longest possible match of the regular expression.
Is it possible to get the shortest possible match?

Example:

string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
string sCommentReg    = "Comment\\((.*)\\)\\,"
Regexp regComment       = regexp sCommentReg


Gives back "Foo1), Issue(Foo2), Location(Foo3". I need only "Foo1".
OK, I know that "Result\\((.*)\\)\\, Issue" would solve the problem, but what if I change the order.

Thank You,

Gabor

Re: Shortest possible match with regexp
llandale - Thu Feb 24 14:46:31 EST 2011

GaborMoizes - Thu Feb 24 12:08:22 EST 2011

I cannot modify the post, so I re-post with modification:

Doors Regexp gives the longest possible match of the regular expression.
Is it possible to get the shortest possible match?

Example:

string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
string sCommentReg    = "Comment\\((.*)\\)\\,"
Regexp regComment       = regexp sCommentReg


Gives back "Foo1), Issue(Foo2), Location(Foo3". I need only "Foo1".
OK, I know that "Result\\((.*)\\)\\, Issue" would solve the problem, but what if I change the order.

Thank You,

Gabor

I didn't try it, but your center <(.*)> means "any number of non-EOL characters", whereas what you want is "any number of non-EOL and non-( characters". Didn't try it, but this might work:
(http://.^\(*).

Nope, almost, its
(^\(*).

string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
// string sCommentReg = "Comment\\((.*)\\)\\,"
string sCommentReg = "Comment\\((^\\(*)\\)\\,"
Regexp regComment = regexp sCommentReg

if (regComment st) print stmatch 1 "\n"
  • Louie

Golly, got a memory about "minimum" and "maximum" expression searches...

Re: Shortest possible match with regexp
llandale - Thu Feb 24 14:52:08 EST 2011

llandale - Thu Feb 24 14:46:31 EST 2011
I didn't try it, but your center <(.*)> means "any number of non-EOL characters", whereas what you want is "any number of non-EOL and non-( characters". Didn't try it, but this might work:
(http://.^\(*).

Nope, almost, its
(^\(*).

string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
// string sCommentReg = "Comment\\((.*)\\)\\,"
string sCommentReg = "Comment\\((^\\(*)\\)\\,"
Regexp regComment = regexp sCommentReg

if (regComment st) print stmatch 1 "\n"

  • Louie

Golly, got a memory about "minimum" and "maximum" expression searches...

Mental note: use Preview. Remember that. Again...

([.^\\(]*)
 
Nope, almost, its 
([^\\(]*)
 
string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
// string sCommentReg    = "Comment\\((.*)\\)\\,"
string sCommentReg      = "Comment\\(([^\\(]*)\\)\\,"
Regexp regComment       = regexp sCommentReg
 
if (regComment st) print st[match 1] "\n"if (regComment st) print stmatch 1 "\n"

 

  • Louie

 

Re: Shortest possible match with regexp
llandale - Thu Feb 24 17:32:03 EST 2011

llandale - Thu Feb 24 14:52:08 EST 2011

Mental note: use Preview. Remember that. Again...

([.^\\(]*)
 
Nope, almost, its 
([^\\(]*)
 
string st = "Comment(Foo1), Issue(Foo2), Location(Foo3),"
// string sCommentReg    = "Comment\\((.*)\\)\\,"
string sCommentReg      = "Comment\\(([^\\(]*)\\)\\,"
Regexp regComment       = regexp sCommentReg
 
if (regComment st) print st[match 1] "\n"if (regComment st) print stmatch 1 "\n"

 

  • Louie

 

Found a reference to "greedy" and "non-greedy" matches here:

http://gnosis.cx/publish/programming/regular_expressions.html

Don't know if that will help.

  • Louie

Re: Shortest possible match with regexp
Mathias Mamsch - Thu Feb 24 18:13:58 EST 2011

Which DOORS version are you using? I wonder if the new regexp2 can do this. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Shortest possible match with regexp
GaborMoizes - Fri Feb 25 02:02:42 EST 2011

llandale - Thu Feb 24 17:32:03 EST 2011
Found a reference to "greedy" and "non-greedy" matches here:

http://gnosis.cx/publish/programming/regular_expressions.html

Don't know if that will help.

  • Louie

Ah, greedy ! That was the keyword! I've found a solution usig regexp2 as Mathias suggested.

string sregComment    = "Comment\\((.*?)\\)\\,"
Regexp regComment       = regexp2 sregComment


Working fine, thank you!